package com.brillilab.starter.controller.aio.method;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.method.MethodSearchTypeEnum;
import com.brillilab.domain.vo.method.MethodSearchVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.method.IMethodSearchService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 方法搜索记录
 *
 * @author WuMenghao
 * @date    2019-03-01
 */
@RestController
@RequestMapping("/api/method/search")
public class MethodSearchController {

    @Resource
    private IMethodSearchService methodSearchService;
    @Resource
    private UsersLogic usersLogic;

    /**
     * 参考方法热搜关键词
     * @return
     */
    @RequestMapping(value="/hot",method=RequestMethod.GET)
    public ResponseVo hotSearchKeyWord(){
        List<MethodSearchVo> rs=methodSearchService.getMethodReferenceHotSearchRecord();
        return ResponseVo.success(rs);
    }

    /**
     * 参考方法搜索历史
     * @param token
     * @return
     */
    @RequestMapping(value="/refer/history",method=RequestMethod.GET)
    public ResponseVo referenceMethodSearchHistory(@RequestHeader(StarterConstant.TOKEN) String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        List<MethodSearchVo> rs=methodSearchService.getMethodReferenceSearchHistory(userInfo.getId());
        return ResponseVo.success(rs);
    }

    /**
     * 方法库搜索历史
     * @param token
     * @return
     */
    @RequestMapping(value="/repo/history",method=RequestMethod.GET)
    public ResponseVo methodRepositorySearchHistory(@RequestHeader(StarterConstant.TOKEN) String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        List<MethodSearchVo> rs=methodSearchService.getMethodRepositorySearchHistory(userInfo.getId());
        return ResponseVo.success(rs);
    }

    /**
     * 清空参考方法搜索历史
     * @param token
     * @return
     */
    @RequestMapping(value="/refer/clear",method=RequestMethod.DELETE)
    public ResponseVo clearReferenceMethodSearchHistory(@RequestHeader(StarterConstant.TOKEN) String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        boolean rs=methodSearchService.clearMethodSearchHistory(userInfo.getId(),MethodSearchTypeEnum.METHOD_REFER.getType());
        return rs ? ResponseVo.success() : ResponseVo.failure();
    }

    /**
     * 清空方法库搜索历史
     * @param token
     * @return
     */
    @RequestMapping(value="/repo/clear",method=RequestMethod.DELETE)
    public ResponseVo clearMethodRepositorySearchHistory(@RequestHeader(StarterConstant.TOKEN) String token){
        Assert.isTrue(StringUtils.isNotBlank(token),"请求参数缺失！");
        UserInfoVo userInfo=usersLogic.getUserByToken(token);
        boolean rs=methodSearchService.clearMethodSearchHistory(userInfo.getId(),MethodSearchTypeEnum.METHOD_REPOSITORY.getType());
        return rs ? ResponseVo.success() : ResponseVo.failure();
    }

}
